package com.diandian.backend.dingding.exchange.controller;

import com.aliyun.dingtalkcontact_1_0.models.ListEmpLeaveRecordsResponse;
import com.aliyun.dingtalkcontact_1_0.models.SearchUserResponse;
import com.diandian.backend.dingding.exchange.service.DingdingTokenService;
import com.diandian.base.boot.starter.response.BaseResult;
import com.diandian.base.boot.starter.util.ResultUtil;
import com.diandian.dingding.boot.starter.service.DingdingApplicationAddressBookUserManageService;
import com.dingtalk.api.response.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value = "/user")
public class DingdingApplicationAddressBookUserController {

    @Autowired
    private DingdingApplicationAddressBookUserManageService dingdingApplicationAddressBookService;

    @Autowired
    private DingdingTokenService dingdingTokenService;

    /**
     * 查询用户详情（根据用户ID获取用户信息）
     */
    @GetMapping("/getUserInfoByUserId")
    public OapiV2UserGetResponse getUserInfoByUserId(@RequestHeader(value = "appKey") String appKey,
                                                     @RequestHeader(value = "appSecret") String appSecret,
                                                     @RequestParam(value = "dingUserId") String dingUserId) throws Exception {
        String token = dingdingTokenService.getToken(appKey, appSecret);
        return dingdingApplicationAddressBookService.getUserInfoByUserId(token, dingUserId);
    }

    /**
     * 获取部门用户基础信息(分页）
     */
    @GetMapping("/getPageDeptUser")
    public OapiUserListsimpleResponse getPageDeptUser(@RequestHeader(value = "appKey") String appKey,
                                                      @RequestHeader(value = "appSecret") String appSecret,
                                                      @RequestParam(value = "dingDeptId") Long dingDeptId,
                                                      @RequestParam(value = "cursor") Long cursor,
                                                      @RequestParam(value = "size") Long size,
                                                      @RequestParam(value = "orderField", required = false) String orderField,
                                                      @RequestParam(value = "containAccessLimit", required = false) Boolean containAccessLimit,
                                                      @RequestParam(value = "language", required = false) String language) throws Exception {
        String token = dingdingTokenService.getToken(appKey, appSecret);
        return dingdingApplicationAddressBookService.getPageDeptUser(token, dingDeptId, cursor, size, orderField, containAccessLimit, language);
    }

    /**
     * 获取部门用户基础信息(全部）
     */
    @GetMapping("/getAllDeptUser")
    public BaseResult getAllDeptUser(@RequestHeader(value = "appKey") String appKey,
                                     @RequestHeader(value = "appSecret") String appSecret,
                                     @RequestParam(value = "dingDeptId") Long dingDeptId,
                                     @RequestParam(value = "orderField", required = false) String orderField,
                                     @RequestParam(value = "containAccessLimit", required = false) Boolean containAccessLimit,
                                     @RequestParam(value = "language", required = false) String language) throws Exception {
        String token = dingdingTokenService.getToken(appKey, appSecret);
        return ResultUtil.makeResult(dingdingApplicationAddressBookService.getAllDeptUser(token, dingDeptId, orderField, containAccessLimit, language));
    }

    /**
     * 获取部门用户userid列表
     */
    @GetMapping("/getDeptUserIdList")
    public OapiUserListidResponse getDeptUserIdList(@RequestHeader(value = "appKey") String appKey,
                                                    @RequestHeader(value = "appSecret") String appSecret,
                                                    @RequestParam(value = "dingDeptId", required = false) Long dingDeptId) throws Exception {
        String token = dingdingTokenService.getToken(appKey, appSecret);
        return dingdingApplicationAddressBookService.getDeptUserIdList(token, dingDeptId);
    }

    /**
     * 获取部门用户详情(分页）
     */
    @GetMapping("/getPageDeptUserDetail")
    public OapiV2UserListResponse getPageDeptUserDetail(@RequestHeader(value = "appKey") String appKey,
                                                        @RequestHeader(value = "appSecret") String appSecret,
                                                        @RequestParam(value = "dingDeptId") Long dingDeptId,
                                                        @RequestParam(value = "cursor") Long cursor,
                                                        @RequestParam(value = "size") Long size,
                                                        @RequestParam(value = "orderField", required = false) String orderField,
                                                        @RequestParam(value = "containAccessLimit", required = false) Boolean containAccessLimit,
                                                        @RequestParam(value = "language", required = false) String language) throws Exception {
        String token = dingdingTokenService.getToken(appKey, appSecret);
        return dingdingApplicationAddressBookService.getPageDeptUserDetail(token, dingDeptId, cursor, size, orderField, containAccessLimit, language);
    }

    /**
     * 获取部门用户详情(全部）
     */
    @GetMapping("/getAllDeptUserDetail")
    public BaseResult getAllDeptUserDetail(@RequestHeader(value = "appKey") String appKey,
                                                        @RequestHeader(value = "appSecret") String appSecret,
                                                        @RequestParam(value = "dingDeptId") Long dingDeptId,
                                                        @RequestParam(value = "orderField", required = false) String orderField,
                                                        @RequestParam(value = "containAccessLimit", required = false) Boolean containAccessLimit,
                                                        @RequestParam(value = "language", required = false) String language) throws Exception {
        String token = dingdingTokenService.getToken(appKey, appSecret);
        return ResultUtil.makeResult(dingdingApplicationAddressBookService.getAllDeptUserDetail(token, dingDeptId, orderField, containAccessLimit, language));
    }

    /**
     * 获取员工人数
     */
    @GetMapping("/getUserCount")
    public BaseResult getUserCount(@RequestHeader(value = "appKey") String appKey,
                                   @RequestHeader(value = "appSecret") String appSecret,
                                   @RequestParam(value = "onlyActive") Boolean onlyActive) throws Exception {
        String token = dingdingTokenService.getToken(appKey, appSecret);
        return ResultUtil.makeResult(dingdingApplicationAddressBookService.getUserCount(token, onlyActive));
    }

    /**
     * 获取未登录钉钉的员工列表
     */
    @GetMapping("/getPageUnLoginUser")
    public OapiInactiveUserV2GetResponse getPageUnLoginUser(@RequestHeader(value = "appKey") String appKey,
                                                            @RequestHeader(value = "appSecret") String appSecret,
                                                            @RequestParam(value = "isActive") Boolean isActive,
                                                            @RequestParam(value = "deptIds", required = false) List<Long> deptIds,
                                                            @RequestParam(value = "offset") Long offset,
                                                            @RequestParam(value = "size") Long size,
                                                            @RequestParam(value = "queryDate") String queryDate) throws Exception {
        String token = dingdingTokenService.getToken(appKey, appSecret);
        return dingdingApplicationAddressBookService.getPageUnLoginUser(token, isActive, deptIds, offset, size, queryDate);
    }

    /**
     * 根据手机号获取用户信息
     */
    @GetMapping("/getUserInfoByMobile")
    public OapiV2UserGetbymobileResponse getByMobile(@RequestHeader(value = "appKey") String appKey,
                                                     @RequestHeader(value = "appSecret") String appSecret,
                                                     @RequestParam(value = "mobile") String mobile) throws Exception {
        String token = dingdingTokenService.getToken(appKey, appSecret);
        return dingdingApplicationAddressBookService.getUserInfoByMobile(mobile, token);
    }

    /**
     * 查询离职记录列表
     * startTime格式：yyyy-MM-dd HH:mm:ss
     * endTime格式：yyyy-MM-dd HH:mm:ss
     */
    @GetMapping("/queryLeaveRecordList")
    public ListEmpLeaveRecordsResponse queryLeaveRecordList(@RequestHeader(value = "appKey") String appKey,
                                                            @RequestHeader(value = "appSecret") String appSecret,
                                                            @RequestParam(value = "startTime") String startTime,
                                                            @RequestParam(value = "endTime") String endTime,
                                                            @RequestParam(value = "nextToken") String nextToken,
                                                            @RequestParam(value = "maxResults") Integer maxResults) throws Exception {
        String token = dingdingTokenService.getToken(appKey, appSecret);
        startTime = startTime.replaceFirst(" ", "T") + "Z";
        endTime = endTime.replaceFirst(" ", "T") + "Z";
        return dingdingApplicationAddressBookService.queryLeaveRecordList(token, startTime, endTime, nextToken, maxResults);
    }

    /**
     * 搜索用户userId
     */
    @GetMapping("/searchUserId")
    public SearchUserResponse searchUserId(@RequestHeader(value = "appKey") String appKey,
                                           @RequestHeader(value = "appSecret") String appSecret,
                                           @RequestParam(value = "queryWord") String queryWord,
                                           @RequestParam(value = "offset") Integer offset,
                                           @RequestParam(value = "size") Integer size,
                                           @RequestParam(value = "fullMatchField",required = false) Integer fullMatchField) throws Exception {
        String token = dingdingTokenService.getToken(appKey, appSecret);
        return dingdingApplicationAddressBookService.searchUserId(token, queryWord, offset, size, fullMatchField);
    }
}
